{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "outputs": [],
   "source": [
    "def factorial(n):\n",
    "    \"\"\"计算n的阶乘\"\"\"\n",
    "    if n == 0 or n == 1:\n",
    "        return 1\n",
    "    else:\n",
    "        result = 1\n",
    "        for i in range(2, n + 1):\n",
    "            result *= i\n",
    "        return result\n",
    "\n",
    "def sum_of_factorials(N):\n",
    "    \"\"\"计算1 + 2! + 3! + ... + N!的值\"\"\"\n",
    "    total = 0\n",
    "    for i in range(1, N + 1):\n",
    "        total += factorial(i)\n",
    "    return total\n",
    "\n",
    "# 输入任意数N\n",
    "N = int(input(\"请输入一个整数N: \"))\n",
    "\n",
    "# 计算并输出结果\n",
    "result = sum_of_factorials(N)\n",
    "print(f\"1 + 2! + 3! + ... + {N}! = {result}\")"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
